package cn.ekuma.pos.pda.qkorder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.openbravo.bean.erp.Order;
import com.openbravo.bean.erp.OrderLine;
import com.openbravo.data.basic.BasicException;
import com.openbravo.pos.util.RoundUtils;

public class OrderManager {
	
	public interface PageChangeListener{
	      public void pageChanged(int pageIndex);
	}

	   private Map<Integer,OrderPageEntry> pages;
	   private Order currentOrder;
	   private OrderPageEntry currentPage;
	   public static int DEFAULT_SELECT_PAGE_INDEX=0;
	   private boolean isEditAble;
	   
	   private PageChangeListener l;
	   
	public OrderManager(){
		 pages=new TreeMap<Integer,OrderPageEntry>();
	        isEditAble=false;
	        currentPage=new OrderPageEntry(DEFAULT_SELECT_PAGE_INDEX);
	        pages.put(DEFAULT_SELECT_PAGE_INDEX, currentPage);
	}
	
	
	public void setOrder(Order order,boolean isEditAble){
	       currentOrder=order;
	       this.isEditAble=isEditAble;
	       pages.clear();
	       currentPage=new OrderPageEntry(DEFAULT_SELECT_PAGE_INDEX);
	       pages.put(DEFAULT_SELECT_PAGE_INDEX, currentPage);  
	       buildPageEntry();
	       firePageChanged();
	   }

	 public boolean isEditAble() {
	        return isEditAble;
	    }

	    public void setEditAble(boolean isEditAble) {
	        this.isEditAble = isEditAble;
	    }

	   //页操作
	   public void pageUp(){
	       Iterator<OrderPageEntry> list=pages.values().iterator();
	       OrderPageEntry before=list.next();
	       OrderPageEntry temp;
	       while(list.hasNext()){
	           temp=list.next();
	           if(this.currentPage.equals(temp)){
	               currentPage=before;
	               firePageChanged();
	               break;
	           }
	           before=temp;
	       }
	   }

	   public void pageDown(){
	       Iterator<OrderPageEntry> list=pages.values().iterator();
	       OrderPageEntry temp;
	       while(list.hasNext()){
	           temp=list.next();
	           if(currentPage.equals(temp)&&list.hasNext()){
	               currentPage=list.next();
	               firePageChanged();
	               break;
	           }
	       }
	   }

	   public void selectPage(int select){
	        OrderPageEntry selectedPage=pages.get(select);
	        if(selectedPage==null){
	           if(!isEditAble())
	               return;
	           currentPage=new OrderPageEntry(select);
	           pages.put(currentPage.getPageIndex(), currentPage);
	           firePageChanged();
	         }else{
	            currentPage=selectedPage;
	            firePageChanged();
	         }
	   }

	   public void pageFirst(){
	       Iterator<OrderPageEntry> list=pages.values().iterator();
	       OrderPageEntry first=list.next();
	       if(!currentPage.equals(first)){
	            currentPage=first;
	            firePageChanged();
	       }
	   }
	   
	   public void pageEnd(){
	       Iterator<OrderPageEntry> list=pages.values().iterator();
	       OrderPageEntry end = list.next();
	       while(list.hasNext()){
	           end=list.next();
	       }
	       if(!this.currentPage.equals(end)){
	            currentPage=end;
	            firePageChanged();
	       }
	   }

	   public int getPageCount(){
	      return pages.size();
	   }

	   public OrderPageEntry getCurrentPage(){
	       return currentPage;
	   }

	   //当前页包装类
	   public void setOrderLine(List<OrderLine> lines){
		   currentPage.setOrderLines(lines);
	   }
	   
	   public OrderLine getLine(int index) {
	        return currentPage.getLine(index);
	    }

	   public Iterable<OrderLine> getLines() {
	        return this.currentPage.getLines();
	    }

	    public void addLine(OrderLine oLine) {
	        currentPage.addLine(oLine);
	    }

	    public void insertLine(int index, OrderLine oLine) {
	        currentPage.insertLine(index, oLine);
	    }

	    public void setLine(int index, OrderLine oLine) {
	       currentPage.setLine(index, oLine);
	    }

	    public void removeLine(int index) {
	       currentPage.removeLine(index);
	    }

	    public int getLinesCount() {
	        return currentPage.getLinesCount();
	    }
	    
	    public OrderPageEntry getPage(int index){
	        return new ArrayList<OrderPageEntry>(pages.values()).get(index);
	    }

	    public int getOrderLinesCount(){
	        int lineCount=0;
	        Iterator<OrderPageEntry> list=pages.values().iterator();
	        while(list.hasNext()){
	            lineCount+=list.next().getLinesCount();
	        }
	        return lineCount;
	    }

	    /**
	     * 事件相关
	     * @param l
	     */

	   public void setPageChangeListener(PageChangeListener l){
	       this.l=l;
	   }

	   public void removePageChangeListener(){
	      this.l=null;
	   }

	   public void firePageChanged() {
		   if(l!=null)
	         l.pageChanged(currentPage.getPageIndex());
	    }
	   
	   private void buildPageEntry() {
	       List<OrderLine> lines=currentOrder.getLines();
	       if(lines!=null){
	           OrderLine line;
	           int linePageIndex;
	           for(int i=0;i<lines.size();i++){
	              line=lines.get(i);
	              linePageIndex=line.getPageIndex();
	              currentPage=pages.get(linePageIndex);
	              if(currentPage==null){
	                  currentPage=new OrderPageEntry(linePageIndex);
	                  pages.put(currentPage.getPageIndex(), currentPage);
	              }
	              currentPage.addLine(line);
	           }
	       }
	    }


	    public List<OrderLine> buildOrderLines(){
	       List<OrderLine> orderLines=new ArrayList();
	       Iterator<OrderPageEntry> list=pages.values().iterator();
	       while(list.hasNext()){
	           orderLines.addAll(list.next().getLines());
	       }
	       return orderLines;
	    }

	    public Order getOrder(){
	        return currentOrder;
	    }
	    
	    public double getOrderPriceTotal() {
	        double priceTotal=0.0;
	        Iterator<OrderPageEntry> list=pages.values().iterator();
	        while(list.hasNext()){
	            OrderPageEntry page=list.next();
	            priceTotal=RoundUtils.add(priceTotal,page.getTotal());
	        }
	        return priceTotal;
	     }

	     public Collection<OrderPageEntry> getPages(){
	         return pages.values();
	     }
}
